import os
import time

from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext import db

from views import ActualView, TemplateHelper
import config

HTTP_DATE_FMT = "%a, %d %b %Y %H:%M:%S GMT"

class BaseHandler(webapp.RequestHandler):
    def __init__(self):
        self.view = ActualView()
        self.template = TemplateHelper()

class NotFoundHandler(BaseHandler):
    def get(self):
        pass

class NotAuthorizedHandler(BaseHandler):
    def get(self):
        self.response.out.write(self.template.render_output('403',
            { 'logout_url':users.create_logout_url('/')}))

class SearchHandler(BaseHandler):
    def get(self):
        keyword = self.request.get('q')
        self.response.out.write(self.template.render_output('base', {
            'app': config.APP,
            'content': self.view.search_help(keyword),
            'breadcrumb': self.view.breadcrumbs(),
            'person': config.CONTACT,
            'sidebar' :self.view.sidebar(),
            }))

class TagHandler(BaseHandler):
    def get(self):
        pass

class ContactHandler(BaseHandler):
    def get(self):
        pass

class RootHandler(BaseHandler):
    def get(self):
        self.response.out.write(self.template.render_output('base', 
                        {'app': config.APP,
                         'content': self.view.all_help(),
                         'breadcrumb': self.view.breadcrumbs(),
                         'person': config.CONTACT,
                         'sidebar' :self.view.sidebar(),
                        }))

class TopicHandler(BaseHandler):
    def get(self):
        k = self.request.get('k')
        self.response.out.write(self.template.render_output('base', {
            'app': config.APP,
            'breadcrumb': self.view.breadcrumbs(k),
            'person': config.CONTACT,
            'content': self.view.topic_help(k),
            'sidebar' :self.view.sidebar(),
            }))

class StaticHandler(BaseHandler):
    def get(self, s1, s2, theme = config.THEME):
        static_file = os.path.join('template', theme, s1, s2)
        x = open(static_file)
        t = x.read()
        x.close()
        last_modified = time.strftime(HTTP_DATE_FMT,
                                    time.gmtime(os.path.getmtime(static_file)))
        self.response.headers['Content-type'] = 'text/css'
        self.response.headers['Cache-Control'] = 'public, max-age=3600'
        self.response.headers['Last-Modified'] = last_modified
        if self.request.headers.get('If-Modified-Since') < last_modified:
            self.response.out.write(t)
        else:
            #self.response.set_status(304)
            self.response.out.write(t)

class QuestionHandler(BaseHandler):
    def get(self):
        k = self.request.get('k')
        self.response.out.write(self.template.render_output('base', {
            'app': config.APP,
            'breadcrumb': self.view.breadcrumbs(k),
            'person': config.CONTACT,
            'content': self.view.question_help(k),
            'sidebar' :self.view.sidebar(),
         }))

class ImageHandler(BaseHandler):
    def get(self):
        k = self.request.get('k')
        image = db.get(db.Key(k))
        self.response.headers['Content-Type'] = 'image/jpeg'
        self.response.out.write(image.pic)